package com.sc.page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sc.bean.GoodsBean;
import com.sc.data.GoodsData;

public class GoodsPage extends HttpServlet {

	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//fix
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String path = req.getContextPath();
		resp.setContentType("text/html");
		
		//req
		
		//data
		
		GoodsData gd = GoodsData.getInstance();
		
		List<GoodsBean> gs = gd.getGoods();
		
		//resp
		PrintWriter w = resp.getWriter();
		
		w.println("<!DOCTYPE html>");
		w.println("<html>");
		w.println("<head>");
		w.println("	<title>商品</title>");
		w.println("	<meta charset='utf-8'>");
		w.println("</head>");
		w.println("<body>");
		w.println("<a href='/sc/goods.html'>逛逛商品</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='SCartPage.jsp'>看看购物车</a>");
		w.println("<table border='1'>");
		w.println("<tr><td>商品编号</td><td>商品名称</td><td>商品价格</td><td>操作</td></tr>");
		w.println("");
		
		for (int i = 0; i < gs.size(); i++) {
			GoodsBean g = gs.get(i);
			w.println("<tr><td>"+g.getId()+"</td><td>"+g.getName()+"</td><td>"+g.getPrice()+"</td><td>   <a href='"+path+"/addscart.html?id="+g.getId()+"'>放入购物车</a>  </td></tr>");
		}
		
		w.println("");
		w.println("");
		w.println("</table>");
		w.println("</body>");
		w.println("</html>");
		
		w.flush();
		w.close();
		
	}
}
